package com.xianniu.ltc.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xianniu.ltc.model.vo.ApiEquipmentVO;
import com.xianniu.ltc.model.vo.ApiTenantDeviceVO;
import com.xianniu.ltc.service.EquipmentApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author WangRuiJie
 * @Title: EquipmentApiServiceImpl
 * @Package com.xianniu.ltc.service.impl
 * @Description:
 * @date 2021/7/311:36 上午
 */
@Slf4j
@Service
public class EquipmentApiServiceImpl implements EquipmentApiService {

    @Override
    public JSONObject selectEquipmentList(JSONObject query, String tenantCode) {
        JSONObject result = new JSONObject();
        JSONObject object = new JSONObject();
        object.put("pageNo", query.getInteger("pageNo"));
        object.put("pageSize", query.getInteger("pageSize"));
        object.put("saasCode", tenantCode);
        JSONObject filter = query.getJSONObject("filter");
        if (Objects.nonNull(filter)){
            JSONObject values = filter.getJSONObject("values");
            object.put("param", values.get("1"));
        }
        String jsonStr = JSON.toJSONString(object);
//        try {
//            HttpResponse response = HttpRequest.post(equipmentUrl).body(jsonStr).execute();
//            result = JSONObject.parseObject(response.body());
//            if (response.getStatus() != 200){
//                log.info("失败-合同添加设备明细-查询资源管理的设备列表：{}", result.getString("msg"));
//            }else {
//                JSONArray jsonArray = result.getJSONArray("data");//把String转换为json
//                List<ApiTenantDeviceVO> temList = jsonArray.toJavaList(ApiTenantDeviceVO.class);
//                List<ApiEquipmentVO> voList = converRecordList(temList);
//                result.put("data", voList);
//                log.info("成功-合同添加设备明细-查询资源管理的设备列表：{}", result);
//            }
//        }catch (Exception e){
//            log.info("异常-合同添加设备明细-查询资源管理的设备列表：{}", e.getMessage());
//        }
        return result;
    }

    @Override
    public JSONArray selectEquipmentById(String id, String tenantCode) {
        JSONArray jsonArray = new JSONArray();
        JSONObject result = new JSONObject();
        JSONObject param = new JSONObject();
        param.put("id", id);
        param.put("saasCode", tenantCode);
        String jsonStr = JSON.toJSONString(param);
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("id", id);
        formMap.put("saasCode", tenantCode);
//        try {
//            HttpResponse response = HttpRequest.get(equipmentIdUrl).form(formMap).execute();
//            result = JSONObject.parseObject(response.body());
//            if (response.getStatus() != 200){
//                log.info("失败-合同添加设备明细-根据ID查询资源管理的设备：{}", result.getString("msg"));
//            }else {
//                JSONObject jsonObject = result.getJSONObject("data");//把String转换为json
//                ApiTenantDeviceVO deviceVO = jsonObject.toJavaObject(ApiTenantDeviceVO.class);
//                jsonArray = converRecordObj(deviceVO);
//                log.info("成功-合同添加设备明细-根据ID查询资源管理的设备：{}", result);
//            }
//        }catch (Exception e){
//            log.info("异常-合同添加设备明细-根据ID查询资源管理的设备：{}", e.getMessage());
//        }
        return jsonArray;
    }


    private List<ApiEquipmentVO> converRecordList(List<ApiTenantDeviceVO> temList){
        List<ApiEquipmentVO> apiList = new ArrayList<>();
        for (ApiTenantDeviceVO deviceVO: temList){
            ApiEquipmentVO vo = new ApiEquipmentVO();
            vo.setShebeiId(String.valueOf(deviceVO.getDeviceId()));
            vo.setShebeifenlei77(deviceVO.getTypeName());
            vo.setShebeijibie13(deviceVO.getLevelName());
            vo.setShebeileixing(deviceVO.getDeviceName());
            vo.setShangpinmingchengpnma(deviceVO.getDeviceName());
            vo.setRbchangshang0(deviceVO.getCnName()+"/"+deviceVO.getEnName());
            vo.setFenleibianma(deviceVO.getTypeCode());
            apiList.add(vo);
        }
        return apiList;
    }

    private JSONArray converRecordObj(ApiTenantDeviceVO obj) {
        JSONArray jsonArray = new JSONArray();
        //通过getDeclaredFields()方法获取对象类中的所有属性（含私有）
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            JSONObject result = new JSONObject();
            //设置允许通过反射访问私有变量
            field.setAccessible(true);
            //获取字段的值
            String value = "";
            try {
                value = field.get(obj).toString();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                log.info("解析设备对象异常:{}", e.getMessage());
            }
            result.put("fillinForce", true);
            result.put("target", field.getName());
            result.put("value", value);
            result.put("whenCreate", true);
            result.put("whenUpdate", true);
            jsonArray.add(result);
        }
        return jsonArray;
    }
}
